import axios from 'axios'
import store from '@/store'
import router from '../router'
import Vue from 'vue'
import cookie from '@/util/cookie'
import {Encrypt} from '@/util/crypto'
import {refreshToken} from '@/api/commonApi'
import {wwwUrl, appId} from '@/config/config'
axios.defaults.timeout = 600000
axios.defaults.withCredentials = false // 跨域请求，允许保存cookie
var loading 

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
		config.headers['x-fuyu-url'] = location.origin + location.pathname
		// 签名
		config.headers['sign'] = Encrypt(new Date().getTime())
		config.headers['loginName'] = store.getters.cookieStart.loginName 
		config.headers['roleCode'] = store.getters.cookieStart.roleCode 
		config.headers['Token'] = store.getters.cookieStart.Token

		config.headers['x-app-id'] = appId

		// config.headers['roleCode'] = 'Teacher'
		// config.headers['Token'] = '53b9f723'
		var authToken = cookie.get('authToken')
		if (authToken) {
			config.headers['Authorization'] = 'Bearer ' + (JSON.parse(authToken)).access_token
		}
	 	loading = Vue.prototype.$loading({
			lock: true,
			text: 'Loading',
			spinner: 'el-icon-loading',
			background: 'rgba(0, 0, 0, 0)'
		})

	return config
}, function (error) {
	console.log('error' + error)
	return Promise.reject(error)
})
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
	loading.close()
	return response
}, function (error) {
	loading.close()
	
	if(!error.response){

		return Promise.reject()
	}
	switch (error.response.status) {
		case 302:
			Vue.prototype.$alert('状态：302，登录过期，请重新登陆', '提示', {
				confirmButtonText: '确定',
				callback: action => {
					location.href = wwwUrl
				}
			  })
		break
		case 401:
			var authToken = cookie.get('authToken')
			var params = {             
				refreshtoken: (JSON.parse(authToken)).refresh_token
			}
			return refreshToken(params).then(res => {
					let domain = '.xueerqin.net'
					cookie.set('authToken', decodeURIComponent(JSON.stringify(res)), 100, domain)
					
					var config = error.config
					config.headers['Authorization'] = 'Bearer ' + res.access_token
					return axios(config)
			})
			break
		case 403:
			var data = error.response.data
			if (data.code == 302) {
				Vue.prototype.$alert('状态：403 => 302，' + data.message, '提示', {
					confirmButtonText: '确定',
					callback: action => {
						location.href = wwwUrl
					}
				})
				return
			}
			Vue.prototype.$message.error(error.response.data.error.message)
		break
		// case 404:
		// 	var data = error.response.data
			
		// 	Vue.prototype.$message.error(error.response.data.error.message)
		// break
		case 500:
			var data = error.response.data
			if(data && data.msg){
				Vue.prototype.$message.error(data.msg)
				return Promise.reject(error.response)
			}
			Vue.prototype.$message.error('状态：500,服务器出错了')
		break
		default:
			var data = error.response.data
			if(data && data.msg){
				Vue.prototype.$message.error(data.msg)
				return
			}

	}
	return Promise.reject(error.response)
})
export default axios
